package com.thecafetechno.main;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.thecafetechno.util.HibernateUtil;

public class DataSelectionHQLExampleWithProjection {
	public static void main(String[] ar) {
		List<String> studentNameList;
		DataPopulator.populateData();
		System.out.println();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		// To select names of all persistant student objects
		studentNameList = session.createQuery(
				"SELECT student.name FROM Student student").list();
		transaction.commit();
		System.out
				.println("Result for : \"SELECT student.name FROM Student student\" ");
		for (String studentName : studentNameList) {
			System.out.println(studentName);
		}

		System.out.println();
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		// this find names of students, where marks are greater than 75
		studentNameList = session
				.createQuery(
						"SELECT student.name from Student student where student.marks>75")
				.list();
		transaction.commit();
		System.out
				.println("Result for : \"SELECT student.name from Student student where student.marks>75\" ");
		for (String studentName : studentNameList) {
			System.out.println(studentName);
		}

		System.out.println();
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		// this find count of students, where marks are greater than 75
		Long studentCount = (Long) session
				.createQuery(
						"SELECT count(student.name) from Student student where student.marks>75")
				.uniqueResult();
		transaction.commit();
		System.out
				.println("Result for : \"SELECT count(student.name) from Student student where student.marks>75\" ");
		System.out.println(studentCount);
	}
}
